package com.caipiao.service.impl;

import com.caipiao.mapper.SIntegralMapper;
import com.caipiao.mapper.SUserMapper;
import com.caipiao.mapper.SUserPlanItemRecordMapper;
import com.caipiao.mapper.SUserWalletRecordMapper;
import com.caipiao.pojo.*;
import com.caipiao.service.WalletRecordService;
import com.caipiao.util.CommonUtils;
import com.caipiao.util.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/9.
 */
@Service
@Transactional
public class WalletRecordServiceImpl implements WalletRecordService {

    private static final Logger LOG = LoggerFactory.getLogger(WalletRecordServiceImpl.class);

    @Autowired
    private SUserWalletRecordMapper sUserWalletRecordMapper;
    @Autowired
    private SUserMapper sUserMapper;
    @Autowired
    private SIntegralMapper sIntegralMapper;

    @Override
    public ObjectResult withdraw(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SUser sUser = sUserMapper.selectByPrimaryKey(Integer.parseInt(userId));

        if(sUser.getIsRealname() != 1){
            return ObjectResult.build("0","未实名认证");
        }

        Float amount = Float.parseFloat(xmlBean.getAmount());

        if (sUser.getAccountAble()<amount){
            return ObjectResult.build("0","提现积分超过可用积分");
        }

        if(amount<100){
            return ObjectResult.build("0","提现不能低于100积分");
        }

        Integer count = sUserWalletRecordMapper.select(userId);
        if(count>0){
            return ObjectResult.build("0","已有在处理中的订单");
        }
        try {

            //订单号
            String rechargeNo = CommonUtils.getRechargeNo("W");

            SUserWalletRecord sUserWalletRecord = new SUserWalletRecord();
            sUserWalletRecord.setStatus(1);
            sUserWalletRecord.setAddTime(new Date());
            sUserWalletRecord.setModifyTime(new Date());
            sUserWalletRecord.setUserId(Integer.parseInt(userId));
            sUserWalletRecord.setRechargeNo(rechargeNo);
            sUserWalletRecord.setPayType(3);
            sUserWalletRecord.setPayStatus(1);
            sUserWalletRecord.setType(2);
            sUserWalletRecord.setValue(amount);
            //添加提现申请记录
            sUserWalletRecordMapper.insertSelective(sUserWalletRecord);
            //修改用户积分
            sUser.setAccountAble(sUser.getAccountAble()-amount);
            sUser.setFrozen(sUser.getFrozen()+amount);
            sUserMapper.updateByPrimaryKeySelective(sUser);

            //积分记录
            Map<String,Object> map = new HashMap<>();
            map.put("userId",Integer.parseInt(userId));
            map.put("integral",0-amount);
            map.put("type",1);
            map.put("planId",0);
            sIntegralMapper.insertIntegralByMap(map);
        }catch (RuntimeException e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            ExceptionUtil.logError(e,LOG);
            return ObjectResult.build("0","清账申请失败");
        }

        return ObjectResult.build("1","清账申请成功");
    }
}
